package com.hdu.other.tanxin;


import java.util.PriorityQueue;


public class lc1792 {
    class Solution {
        public double maxAverageRatio(int[][] classes, int extraStudents)
        {
            PriorityQueue<ClassInfo> pq = new PriorityQueue<>((a, b) -> Double.compare(b.getDelta(), a.getDelta()));
            for (int[] c : classes) {
                pq.offer(new ClassInfo(c[0], c[1]));
            }
            while (extraStudents > 0) {
                ClassInfo ci = pq.poll();
                ci.add();
                pq.offer(ci);
                extraStudents--;
            }
            double ans = 0;
            while (!pq.isEmpty()) {
                ans += pq.poll().getRatio();
            }
            return ans / classes.length;
        }

        class ClassInfo
        {
            int pass;
            int total;

            public ClassInfo(int pass, int total)
            {
                this.pass = pass;
                this.total = total;
            }

            double getRatio()
            {
                return (double) pass / total;
            }

            double getDelta() {
                return (double) (pass + 1) / (total + 1) - getRatio();
            }

            void add() {
                pass++;
                total++;
            }
        }
    }
}
